package com.cloudcreativity.blyg.utils;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.RadioGroup;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.databinding.LayoutDialogCheckBinding;

import java.math.BigDecimal;

public class CheckDialog extends Dialog {

    public ObservableField<String> stock = new ObservableField<>();

    private boolean isAdd = true;
    private OnOkListener onOkListener;

    public void setOnOkLisntener(OnOkListener onOkListener) {
        this.onOkListener = onOkListener;
    }

    public CheckDialog(@NonNull Context context) {
        super(context, R.style.myProgressDialogStyle);
        setCanceledOnTouchOutside(false);
        LayoutDialogCheckBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.layout_dialog_check,
                null,
                false);
        setContentView(binding.getRoot());
        binding.setUtils(this);
    }

    public void onClose(){
        KeyBoardHelper.close(getContext(),this);
        dismiss();
    }

    public void onSave(){
        if(onOkListener!=null){
            if(TextUtils.isEmpty(stock.get())){
                return;
            }
            double doubleValue = new BigDecimal(stock.get()).doubleValue();
            onOkListener.onOk(isAdd,doubleValue);
        }
        KeyBoardHelper.close(getContext(),this);
        dismiss();
    }

    public void onCheckChange(RadioGroup group,int checkedId){
        switch (group.getCheckedRadioButtonId()){
            case R.id.rb_add:
                isAdd = true;
                break;
            case R.id.rb_reduce:
                isAdd = false;
                break;
        }
    }

    public interface OnOkListener{
        void onOk(boolean isAdd,double stock);
    }
}
